package com.woniuxy.mapper;

import com.woniuxy.entity.Address;
import com.woniuxy.entity.Community;
import com.woniuxy.entity.Picture;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface CommunityMapper {
    List<Community> selectCommunityList();
    @Select("select count(distinct com_id) from address")
    Integer selectCount();
    @Select("SELECT COUNT(DISTINCT CONCAT(com_id, number)) from address")
    Integer selectRoomCount();
    @Select("select count(distinct user_id) from address ")
    Integer selectInhabitedCount();

    List<Community> selectCommunityListbyAssess();

    List<Community> selectCommunityListByAddress(Address addressEntity);
    @Select("SELECT * FROM picture WHERE com_id = #{comId}")
    List<Picture> selectCommunityImgs(Integer comId);

    Community selectCommunityById(Integer comId);
    @Select("SELECT * FROM address where com_id =#{comId}  limit 1;")
    Address getCommunityAddress(Integer comId);
    @Update("update address set user_id = #{userId},check_in_time = #{checkInTime}, check_out_time = #{checkOutTime} where add_id = #{addId}")
    void checkIn( @Param("addId") Integer addId,@Param("checkInTime") LocalDate checkInTime,@Param("checkOutTime") LocalDate checkOutTime,@Param("userId") Integer userId);
    @Select("select add_id from address where check_out_time < now() and user_id is not null")
    List<Integer> selectCommunityIdList();
    @Update("update address set user_id =null,check_out_time = null,check_in_time =null where add_id = #{addId}")
    void updateCommunity(Integer addId);


    /**
     * 查询已经入住的小区列表
     * select c.com_id ,c.com_name,c.describe,c.assess,c.phone,
     *        a.add_id,a.user_id,a.market,a.region,a.details,a.floor,a.number,a.bed,a.price,a.check_in_time,a.check_out_time,
     *        u.name
     *        from community c
     *            join address a on c.com_id = a.com_id
     *            join user u on a.user_id = u.user_id;
     */
}
